package com.example.droideep.fragment;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.example.droideep.R;
import com.example.droideep.common.activities.SimpleFragmentBase;

import butterknife.InjectView;
import droideep.library.utils.MetricsConverter;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class DynamicCompleteInfoFragment extends SimpleFragmentBase {

    @InjectView(R.id.bottom)
    RelativeLayout bottom;
    @InjectView(R.id.detail)
    ViewGroup detail;
    LayoutInflater inflater;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        inflater = LayoutInflater.from(activity);
    }

    @Override
    protected int getContentResId() {
        return R.layout.fragment_dynamic_complete_infos;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        addBottomView(false);
    }

    /**
     * 为{@link #bottom} 添加控件
     *
     * @param animate 　是否添加动画
     */
    private void addBottomView(boolean animate) {
        LayoutParams rlp = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        final View view = createView(bottom);
        final float left = MetricsConverter.dpToPx(getActivity().getApplicationContext(), 8.0f);
        final float right = MetricsConverter.dpToPx(getActivity().getApplicationContext(), 8.0f);
        rlp.setMargins((int) left, 0, (int) right, 0);
        if (!animate) {
            bottom.addView(view, rlp);
        } else {
            LayoutTransition transition = new LayoutTransition();
            Animator animator = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 1.0f);
            transition.setAnimator(LayoutTransition.APPEARING, animator);
            transition.setDuration(300l);
            transition.setStartDelay(LayoutTransition.APPEARING, 0L);
            bottom.setLayoutTransition(transition);
            bottom.addView(view, rlp);

        }

        view.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int height = 0;
                final int count = detail.getChildCount();
                View child;
                for (int i = 0; i < count; i++) {
                    child = detail.getChildAt(i);
                    MarginLayoutParams mlp = (MarginLayoutParams) child.getLayoutParams();
                    height += child.getHeight() + mlp.bottomMargin + mlp.topMargin;
                }
                final int top = view.getTop();
                ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, height - top);
                animator.setDuration(1000L);
                animator.start();
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        bottom.removeView(view);
                        detail.addView(createView(detail));
                        addBottomView(true);
                    }
                });
            }
        });
    }

    private View createView(ViewGroup root) {
        return inflater.inflate(R.layout.layout_personal_info, root, false);
    }

}
